import axios from 'axios';
import * as actionTypes from './actionTypes';

export const getHomeInfo = () => {
  return (dispatch) => {
    axios.get('api/home.json').then((res) => {
      const result = res.data.data;
      dispatch(changeHomeData(result));
    })
  }
}

export const getMoreList = (page) => {
  return (dispatch) => {
    axios.get('api/homeList.json?page='+page).then((res) => {
      const result = res.data.data;
      dispatch(addHomeList(result, page+1));
    })
  }
}

export const toggleShow = (show) => {
  return {
    type: actionTypes.TOGGLE_SCROLL_SHOW,
    show
  }
}

const changeHomeData = (result) => ({
  type: actionTypes.CHANGE_HOME_DATA,
  topList: result.topicList,
  articleList: result.articleList,
  recommendList: result.recommendList
})

const addHomeList = (result, page) => ({
  type: actionTypes.ADD_HOME_LIST,
  list: result,
  page
})